package io.gitee.xyuesheng.day02;

import org.junit.Test;

import java.util.Arrays;

public class DemoAlgorithm {
    @Test
    public void testFoo() {
        int[] array = {23, 5, 8, 88, 77, 55, 33, 99};

        // testSelectionSort(array);
        testBubbleSort(array);
    }

    public void testSelectionSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int min = i;

            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[min]) {
                    min = j;
                }
            }

            if (min != i) {
                int temp = array[i];
                array[i] = array[min];
                array[min] = temp;
            }
        }

        System.out.println(Arrays.toString(array));
    }

    public void testBubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j];
                    array[j] = array[j + 1];

                    array[j + 1] = tmp;
                }
            }
        }

        System.out.println(Arrays.toString(array));
    }
}
